package cn.iocoder.yudao.module.system.service.pushManage;

import cn.iocoder.yudao.framework.common.pojo.EmailInfo;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.date.DateUtils;
import cn.iocoder.yudao.framework.common.util.encryption.EncryptionUtil;
import cn.iocoder.yudao.framework.common.util.http.HttpUtils;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils;
import cn.iocoder.yudao.module.member.api.company.CompanyApi;
import cn.iocoder.yudao.module.member.api.company.dto.CompanyUserRespDTO;
import cn.iocoder.yudao.module.student.api.student.StudentApi;
import cn.iocoder.yudao.module.student.api.student.dto.WebStudentPageReqDTO;
import cn.iocoder.yudao.module.student.api.student.dto.WebStudentRespDTO;
import cn.iocoder.yudao.module.system.api.message.MessageApi;
import cn.iocoder.yudao.module.system.controller.admin.message.vo.MessageCreateReqVO;
import cn.iocoder.yudao.module.system.controller.admin.pushManage.vo.PushManageCreateReqVO;
import cn.iocoder.yudao.module.system.controller.admin.pushManage.vo.PushManageExportReqVO;
import cn.iocoder.yudao.module.system.controller.admin.pushManage.vo.PushManagePageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.pushManage.vo.PushManageUpdateReqVO;
import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.UserPageReqVO;
import cn.iocoder.yudao.module.system.convert.pushManage.PushManageConvert;
import cn.iocoder.yudao.module.system.dal.dataobject.pushManage.PushManageDO;
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
import cn.iocoder.yudao.module.system.dal.mysql.pushManage.PushManageMapper;
import cn.iocoder.yudao.module.system.enums.sms.SmsSceneEnum;
import cn.iocoder.yudao.module.system.service.message.MessageService;
import cn.iocoder.yudao.module.system.service.sms.SmsSendService;
import cn.iocoder.yudao.module.system.service.user.AdminUserService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.*;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.PUSH_MANAGE_NOT_EXISTS;

/**
 * 推送管理 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
@Slf4j
public class PushManageServiceImpl implements PushManageService {

    @Resource
    private PushManageMapper pushManageMapper;
    @Resource
    private MessageApi messageApi;
    @Resource
    private MessageService messageService;
    @Resource
    private StudentApi studentApi;
    @Resource
    private AdminUserService adminUserService;
    @Resource
    private SmsSendService smsSendService;
    @Resource
    private CompanyApi companyApi;
    @Resource
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;
    @Value("${spring.profiles.active}")
    private String active;
    @Override
    @TenantIgnore
    public Long createPushManage(PushManageCreateReqVO createReqVO) {
        // 插入
        PushManageDO pushManage = PushManageConvert.INSTANCE.convert(createReqVO);
        pushManage.setCreator(WebFrameworkUtils.getLoginUserName());
        pushManage.setUpdater(WebFrameworkUtils.getLoginUserName());
        pushManageMapper.insert(pushManage);
        // 返回
        return pushManage.getId();
    }

    @Override
    @TenantIgnore
    public void updatePushManage(PushManageUpdateReqVO updateReqVO) {
        // 校验存在
        this.validatePushManageExists(updateReqVO.getId());
        // 更新
        if(updateReqVO.getStatus().equals("1")){
            updateReqVO.setActionNum(0);
        }
        PushManageDO updateObj = PushManageConvert.INSTANCE.convert(updateReqVO);
        updateObj.setUpdater(WebFrameworkUtils.getLoginUserName());
        pushManageMapper.updateById(updateObj);
    }
    @Override
    @TenantIgnore
    public void updatePushManage(PushManageDO pushManageDO) {
        pushManageDO.setUpdater(WebFrameworkUtils.getLoginUserName());
        pushManageMapper.updateById(pushManageDO);
    }
    @Override
    @TenantIgnore
    public void deletePushManage(Long id) {
        // 校验存在
        this.validatePushManageExists(id);
        // 删除
        pushManageMapper.deleteById(id);
    }

    private void validatePushManageExists(Long id) {
        if (pushManageMapper.selectById(id) == null) {
            throw exception(PUSH_MANAGE_NOT_EXISTS);
        }
    }

    @Override
    @TenantIgnore
    public PushManageDO getPushManage(Long id) {
        return pushManageMapper.selectById(id);
    }

    @Override
    @TenantIgnore
    public List<PushManageDO> getPushManageList(Collection<Long> ids) {
        return pushManageMapper.selectBatchIds(ids);
    }

    @Override
    @TenantIgnore
    public PageResult<PushManageDO> getPushManagePage(PushManagePageReqVO pageReqVO) {
        return pushManageMapper.selectPage(pageReqVO);
    }

    @Override
    @TenantIgnore
    public List<PushManageDO> getPushManageList(PushManageExportReqVO exportReqVO) {
        return pushManageMapper.selectList(exportReqVO);
    }

    @Override
    @TenantIgnore
    @Async
    public Long nowPushMessage(Long id) {
        //单次推送
        PushManageDO pushManageDO=pushManageMapper.selectById(id);
        singlePushManage(pushManageDO);
        return 1l;
    }

    @Override
    @TenantIgnore
    public Long pushMessage(Long id) {
        //单次推送
        List<PushManageDO> pushManageDOS=null;
        if(null==id){
            pushManageDOS=pushManageMapper.selectListByNowDate(new Date());
        }else{
            pushManageDOS=pushManageMapper.selectList("id",id);
        }
        Long num=0L;
        for (PushManageDO pushManageDO:pushManageDOS){
            if(pushManageDO.getType()==1L){//单次推送
                if(pushManageDO.getActionNum()>=1){//已经推送的 直接跳过
                    continue;
                }
            }else if(pushManageDO.getType()==2L){
                if(pushManageDO.getActionFrequency()==0L){//每天
                    if(pushManageDO.getOldPushTime()!=null){
                        long difference=DateUtils.getDays(DateUtils.format(pushManageDO.getOldPushTime()),DateUtils.format(new Date()));
                        if(difference<=0){//最后推送时间是当天则跳过
                            continue;
                        }
                    }
                    if(pushManageDO.getActionTime()!=DateUtils.getHourOfDay()){
                        continue;
                    }
                }else if(pushManageDO.getActionFrequency()==1L) {//每周
                    if (pushManageDO.getActionDay().equals(DateUtils.getWeek(new Date()))) {//执行周几和今天是否匹配
                        long difference = DateUtils.getDays(DateUtils.format(pushManageDO.getOldPushTime()), DateUtils.format(new Date()));
                        if (difference <= 7) {//最后推送时间不到一个星期跳过
                            continue;
                        }
                        if(pushManageDO.getActionTime()!=DateUtils.getHourOfDay()){
                            continue;
                        }
                    }else {
                        continue;
                    }
                }

            }

            singlePushManage(pushManageDO);
            if(null==pushManageDO.getActionNum()){
                pushManageDO.setActionNum(0);
            }
            pushManageDO.setActionNum(pushManageDO.getActionNum()+1);
            pushManageDO.setOldPushTime(new Date());
            num++;
            pushManageMapper.updateById(pushManageDO);
        }

        return num;
    }

    private void singlePushManage(PushManageDO pushManageDO) {
        //获得发送ID PushObjectId
        JSONArray jsonArray = JSONArray.parseArray(pushManageDO.getMessageType());
        //发送
        for (int i = 0; i < jsonArray.size(); i++) {
            Integer id = (Integer) jsonArray.get(i);
            if(id.equals(0)){
                messagePush(pushManageDO);
            }
            if(id.equals(2)){
                emailPush(pushManageDO);
            }
        }
    }
   private void messagePush(PushManageDO pushManageDO){
       boolean isSms=pushManageDO.getIsSms().equals(1);
       //站内信发哪个类型
        //判断发给谁?全部、企业还是学生
        if (pushManageDO.getPushObject() == 0) {//学生和企业
            //发送学生
            int pageNo=1;
            while (true){
                WebStudentPageReqDTO userPageReqVO=new WebStudentPageReqDTO();
                userPageReqVO.setPageNo(pageNo);
                userPageReqVO.setPageSize(100);
                PageResult<WebStudentRespDTO> webStudentPage=studentApi.getWebStudentPage(userPageReqVO);
                List<WebStudentRespDTO> studentRespDTOList=webStudentPage.getList();
                if(CollectionUtils.isEmpty(studentRespDTOList)){
                    break;
                }
                for (int i = 0; i < studentRespDTOList.size(); i++) {
                    WebStudentRespDTO webStudentRespDTO =  studentRespDTOList.get(i);
                    Long id=webStudentRespDTO.getId();
                    sendmessage(pushManageDO.getPushModule(),new Long(id),null,pushManageDO.getTitle(),pushManageDO.getMessageText());
                    if(isSms) {
                        sendSms(0, webStudentRespDTO.getMobile(), webStudentRespDTO.getStudentName());
                    }
                }
                pageNo++;
            }
            //发送企业
//            int pageNo1=1;
//            while (true){
//                UserPageReqVO userPageReqVO=new UserPageReqVO();
//                userPageReqVO.setPageNo(pageNo1);
//                userPageReqVO.setPageSize(100);
//                PageResult<AdminUserDO> adminUserDOPageResult=adminUserService.getUserPage(userPageReqVO);
//                List<AdminUserDO> adminUserDOList=adminUserDOPageResult.getList();
//                if(CollectionUtils.isEmpty(adminUserDOList)){
//                    break;
//                }
//                for (int i = 0; i < adminUserDOList.size(); i++) {
//                    AdminUserDO adminUserDO =  adminUserDOList.get(i);
//                    Long id=adminUserDO.getId();
//                    sendmessage(pushManageDO.getPushModule(),null,new Long(id),pushManageDO.getTitle(),pushManageDO.getMessageText());
//                    if(isSms) {
//                        sendSms(0, adminUserDO.getMobile(), adminUserDO.getNickname());
//                    }
//                }
//                pageNo1++;
//            }
        } else if (pushManageDO.getPushObject() == 1) {//企业
            //发送全部企业/学生还是部分企业学生
//            if (pushManageDO.getPushObjectType() == 0) {//发送全部企业
//                //发送
//                int pageNo=1;
//                while (true){
//                    UserPageReqVO userPageReqVO=new UserPageReqVO();
//                    userPageReqVO.setPageNo(pageNo);
//                    userPageReqVO.setPageSize(100);
//                    PageResult<AdminUserDO> adminUserDOPageResult=adminUserService.getUserPage(userPageReqVO);
//                    List<AdminUserDO> adminUserDOList=adminUserDOPageResult.getList();
//                    if(CollectionUtils.isEmpty(adminUserDOList)){
//                        break;
//                    }
//                    for (int i = 0; i < adminUserDOList.size(); i++) {
//                        AdminUserDO adminUserDO =  adminUserDOList.get(i);
//                        Long id=adminUserDO.getId();
//                        sendmessage(pushManageDO.getPushModule(),null,new Long(id),pushManageDO.getTitle(),pushManageDO.getMessageText());
//                        if(isSms) {
//                            sendSms(0, adminUserDO.getMobile(), adminUserDO.getNickname());
//                        }
//                    }
//                    pageNo++;
//                }
//            } else if (pushManageDO.getPushObjectType() == 1) {//发送部分企业
////                //获得发送ID PushObjectId
//                JSONArray jsonArray = JSONArray.parseArray(pushManageDO.getPushObjectId());
//
//                //发送
//                List<Long> ids=new ArrayList<>();
//                for (int i = 0; i < jsonArray.size(); i++) {
//                    Integer idd = (Integer) jsonArray.get(i);
//                    Long id=idd.longValue();
//                    ids.add(id);
//                    sendmessage(pushManageDO.getPushModule(),id,null,pushManageDO.getTitle(),pushManageDO.getMessageText());
//                }
//                if(isSms) {
//                    List<AdminUserDO> adminUserDOList=adminUserService.getUsers(ids);
//                    for (AdminUserDO adminUserDO:adminUserDOList) {
//                        sendSms(0, adminUserDO.getMobile(), adminUserDO.getNickname());
//                    }
//                }
//            }
        } else if (pushManageDO.getPushObject() == 2) {//学生
            //发送全部企业/学生还是部分企业学生
            if (pushManageDO.getPushObjectType() == 0) {//发送全部学生
                //发送
                int pageNo=1;
                while (true){
                    WebStudentPageReqDTO userPageReqVO=new WebStudentPageReqDTO();
                    userPageReqVO.setPageNo(pageNo);
                    userPageReqVO.setPageSize(100);
                    PageResult<WebStudentRespDTO> webStudentPage=studentApi.getWebStudentPage(userPageReqVO);
                    List<WebStudentRespDTO> studentRespDTOList=webStudentPage.getList();
                    if(CollectionUtils.isEmpty(studentRespDTOList)){
                        break;
                    }
                    for (int i = 0; i < studentRespDTOList.size(); i++) {
                        WebStudentRespDTO webStudentRespDTO =  studentRespDTOList.get(i);
                        Long id=webStudentRespDTO.getId();
                        sendmessage(pushManageDO.getPushModule(),new Long(id),null,pushManageDO.getTitle(),pushManageDO.getMessageText());
                        if(isSms) {
                            sendSms(0, webStudentRespDTO.getMobile(), webStudentRespDTO.getNickname());
                        }
                    }
                    pageNo++;
                }
            } else if (pushManageDO.getPushObjectType() == 1) {//发送部分学生
                //获得发送ID PushObjectId
                JSONArray jsonArray = JSONArray.parseArray(pushManageDO.getPushObjectId());
                List<Long> ids=new ArrayList<>();
                //发送
                for (int i = 0; i < jsonArray.size(); i++) {
                    Integer idd = (Integer) jsonArray.get(i);
                    Long id=idd.longValue();
                    ids.add(id);
                    sendmessage(pushManageDO.getPushModule(),new Long(id),null,pushManageDO.getTitle(),pushManageDO.getMessageText());
                }
                if(isSms) {
                    List<WebStudentRespDTO> studentRespDTOList=studentApi.getWebStudentByIds(ids);
                    for (WebStudentRespDTO studentRespDTO:studentRespDTOList) {
                        sendSms(0, studentRespDTO.getMobile(), studentRespDTO.getNickname());
                    }
                }
            }
        }
    }
  private void emailPush(PushManageDO pushManageDO){//邮箱
      boolean isSms=pushManageDO.getIsSms().equals(1);
     String str = "<!DOCTYPE html><html><head><meta charset='UTF-8'></head><body>"
             + "<p style='text-align:left; padding-right: 20px;'>"+pushManageDO.getMessageText()+"</p>"
             + "<p style='text-align:right; padding-right: 20px;'> <img  width='200px' src='https://demo-oss.oss-cn-beijing.aliyuncs.com/jpg/9fad169ed78a492080a3800a41d97b68.jpg'/></p>"
             + "<p style='text-align:right; padding-right: 20px;'>电话：0755-23372612/0755-23372649</p>"
             + "<p style='text-align:right; padding-right: 20px;'>contact@datarer.com</p>"
             + "<p style='text-align:right; padding-right: 20px;'>www.datarer.com</p>"
             + "</body></html>";
     EmailInfo emailInfo=new EmailInfo();
     emailInfo.setSubject("【数据学徒】"+pushManageDO.getTitle());
     emailInfo.setContent(str);
     //判断发给谁?全部、企业还是学生
     if (pushManageDO.getPushObject() == 0) {//全部
         //发送学生
         int pageNo=1;
         while (true){
             WebStudentPageReqDTO userPageReqVO=new WebStudentPageReqDTO();
             userPageReqVO.setPageNo(pageNo);
             userPageReqVO.setPageSize(100);
             PageResult<WebStudentRespDTO> webStudentPage=studentApi.getWebStudentPage(userPageReqVO);
             List<WebStudentRespDTO> studentRespDTOList=webStudentPage.getList();
             if(CollectionUtils.isEmpty(studentRespDTOList)){
                 break;
             }
             for (int i = 0; i < studentRespDTOList.size(); i++) {
                 WebStudentRespDTO webStudentRespDTO =  studentRespDTOList.get(i);
                 if(StringUtils.isBlank(webStudentRespDTO.getEmail())){
                     continue;
                 }
                 emailInfo.setToUser(webStudentRespDTO.getEmail());
                 sendHtmlMail(emailInfo);
                 if(isSms) {
                     sendSms(3, webStudentRespDTO.getMobile(), webStudentRespDTO.getEmail());
                 }
             }
             pageNo++;
         }
         //发送企业
         int pageNo1=1;
         while (true){
             UserPageReqVO userPageReqVO=new UserPageReqVO();
             userPageReqVO.setPageNo(pageNo1);
             userPageReqVO.setPageSize(100);
             userPageReqVO.setId(1L);
             PageResult<AdminUserDO> adminUserDOPageResult=adminUserService.getUserPage(userPageReqVO);
             List<AdminUserDO> adminUserDOList=adminUserDOPageResult.getList();
             if(CollectionUtils.isEmpty(adminUserDOList)){
                 break;
             }
             for (int i = 0; i < adminUserDOList.size(); i++) {
                 AdminUserDO adminUserDO =  adminUserDOList.get(i);
                 if(StringUtils.isBlank(adminUserDO.getEmail())){
                     continue;
                 }
                 emailInfo.setToUser(adminUserDO.getEmail());
                 sendHtmlMail(emailInfo);
                 if(isSms) {
                     sendSms(3, adminUserDO.getMobile(), adminUserDO.getEmail());
                 }
             }
             pageNo1++;
         }

     } else if (pushManageDO.getPushObject() == 1) {//企业
         //发送全部企业/学生还是部分企业学生
         if (pushManageDO.getPushObjectType() == 0) {//发送全部企业
             //发送企业
             int pageNo1=1;
             while (true){
                 UserPageReqVO userPageReqVO=new UserPageReqVO();
                 userPageReqVO.setPageNo(pageNo1);
                 userPageReqVO.setPageSize(100);
                 userPageReqVO.setId(1L);
                 PageResult<AdminUserDO> adminUserDOPageResult=adminUserService.getUserPage(userPageReqVO);
                 List<AdminUserDO> adminUserDOList=adminUserDOPageResult.getList();
                 if(CollectionUtils.isEmpty(adminUserDOList)){
                     break;
                 }
                 for (int i = 0; i < adminUserDOList.size(); i++) {
                     AdminUserDO adminUserDO =  adminUserDOList.get(i);
                     if(StringUtils.isBlank(adminUserDO.getEmail())){
                         continue;
                     }
                     emailInfo.setToUser(adminUserDO.getEmail());
                     sendHtmlMail(emailInfo);
                     if(isSms) {
                         sendSms(3, adminUserDO.getMobile(), adminUserDO.getEmail());
                     }
                 }

                 pageNo1++;
             }

         } else if (pushManageDO.getPushObjectType() == 1) {//发送部分企业
             //获得发送ID PushObjectId
             JSONArray jsonArray = JSONArray.parseArray(pushManageDO.getPushObjectId());
             //发送
             for (int i = 0; i < jsonArray.size(); i++) {
                 Integer id=(Integer) jsonArray.get(i);
                 AdminUserDO adminUserDO = adminUserService.getUser(id.longValue());
                 if(StringUtils.isBlank(adminUserDO.getEmail())){
                     continue;
                 }
                 emailInfo.setToUser(adminUserDO.getEmail());
                 sendHtmlMail(emailInfo);
                 if(isSms) {
                     sendSms(3, adminUserDO.getMobile(), adminUserDO.getEmail());
                 }
             }
         }
     } else if (pushManageDO.getPushObject() == 2) {//学生
         //发送全部企业/学生还是部分企业学生
         if (pushManageDO.getPushObjectType() == 0) {//发送全部学生
             //发送
             int pageNo=1;
             while (true){
                 WebStudentPageReqDTO userPageReqVO=new WebStudentPageReqDTO();
                 userPageReqVO.setPageNo(pageNo);
                 userPageReqVO.setPageSize(100);
                 PageResult<WebStudentRespDTO> webStudentPage=studentApi.getWebStudentPage(userPageReqVO);
                 List<WebStudentRespDTO> studentRespDTOList=webStudentPage.getList();
                 if(CollectionUtils.isEmpty(studentRespDTOList)){
                     break;
                 }
                 for (int i = 0; i < studentRespDTOList.size(); i++) {
                     WebStudentRespDTO webStudentRespDTO =  studentRespDTOList.get(i);
                     if(StringUtils.isBlank(webStudentRespDTO.getEmail())){
                         continue;
                     }
                     emailInfo.setToUser(webStudentRespDTO.getEmail());
                     sendHtmlMail(emailInfo);
                     if(isSms) {
                         sendSms(3, webStudentRespDTO.getMobile(), webStudentRespDTO.getEmail());
                     }
                 }
                 pageNo++;
             }
         } else if (pushManageDO.getPushObjectType() == 1) {//发送部分学生
             //获得发送ID PushObjectId
             JSONArray jsonArray = JSONArray.parseArray(pushManageDO.getPushObjectId());
             List<Long> ids=new ArrayList<>();
             //发送
             for (int i = 0; i < jsonArray.size(); i++) {
                 Integer id=(Integer) jsonArray.get(i);
                 ids.add(id.longValue());
             }
             List<WebStudentRespDTO> webStudentRespDTOList=studentApi.getWebStudentByIds(ids);
             //组装参数
             for (WebStudentRespDTO webStudentRespDTO:webStudentRespDTOList){
                 if(StringUtils.isBlank(webStudentRespDTO.getEmail())){
                     continue;
                 }
                 emailInfo.setToUser(webStudentRespDTO.getEmail());
                 sendHtmlMail(emailInfo);
                 if(isSms) {
                     sendSms(3, webStudentRespDTO.getMobile(), webStudentRespDTO.getEmail());
                 }
             }
         }
     }

 }

    @Async
    void sendSms(Integer messageType,String mobile,String name){

         Map<String, Object> templateParams=new HashMap<>();
         templateParams.put("date", DateUtils.format(new Date(),DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND));
         if(messageType.equals(0) && StringUtils.isNotBlank(mobile)){
             SmsSceneEnum sceneEnum = SmsSceneEnum.getCodeByScene(5);
             templateParams.put("name", name);
             if(mobile.length()>20){
                 mobile=EncryptionUtil.desDecrypt(mobile);
             }
             smsSendService.sendSingleSms(mobile, null, null,
                    sceneEnum.getTemplateCode(), templateParams);

            }
            if(messageType.equals(3) && StringUtils.isNotBlank(mobile)){
                if(mobile.length()>20){
                    mobile=EncryptionUtil.desDecrypt(mobile);
                }
                SmsSceneEnum sceneEnum = SmsSceneEnum.getCodeByScene(6);
                templateParams.put("name", name);
                templateParams.put("mail", name);
                smsSendService.sendSingleSms(mobile, null, null,
                         sceneEnum.getTemplateCode(), templateParams);
             }
     }
    /**
     * 异步发送邮件
     * @param emailInfo
     */
    void sendHtmlMail(EmailInfo emailInfo){
//        MailUtils.sendHtmlMail(emailInfo);
        emailInfo.setEnvironment(active);
        log.info(JSONObject.toJSONString(emailInfo));
        threadPoolTaskExecutor.execute(()->{
            HttpUtils.doPost("http://127.0.0.1:8085/demo/task/sendMail",JSONObject.toJSONString(emailInfo));
        });
    }
    /**
     * 异步发送站内信
     * @param type
     * @param title
     * @param messageText
     */
    @Async
    void sendmessage(Long type, Long studentId,Long userId, String title, String messageText){
        MessageCreateReqVO messageCreateReqVO=new MessageCreateReqVO();
        messageCreateReqVO.setPhone("");
        messageCreateReqVO.setType(type);
        messageCreateReqVO.setStatus("5");
        messageCreateReqVO.setStudentId(studentId);
        messageCreateReqVO.setStudentName("系统管理员");
        messageCreateReqVO.setTitle(title);
        messageCreateReqVO.setRemark(messageText);
        if(userId==null){
            messageCreateReqVO.setStudentId(studentId);
            messageService.createMessage(messageCreateReqVO);
        }else{
            List<CompanyUserRespDTO> companyUserRespDTOList=companyApi.getCompanyUserByCompanyId(userId);
            for (CompanyUserRespDTO companyUserRespDTO:companyUserRespDTOList) {
                messageCreateReqVO.setUserId(companyUserRespDTO.getUserId());
                messageService.createMessage(messageCreateReqVO);
            }
        }

    }
    public static void main(String[] args) {
        String s="[1,2,3,4,5,6,7]";
        JSONArray jsonArray=JSONArray.parseArray(s);
        for (int i = 0; i < jsonArray.size(); i++) {
            Integer id = (Integer) jsonArray.get(i);
            System.out.println(id);
        }
    }
}
